package com.ha.controller.admin;

import com.ha.constant.JwtClaimsConstant;
import com.ha.context.BaseContext;
import com.ha.dto.EmployeeDTO;
import com.ha.dto.EmployeeLoginDTO;
import com.ha.dto.EmployeePageQueryDTO;
import com.ha.dto.PasswordEditDTO;
import com.ha.entity.Employee;
import com.ha.properties.JwtProperties;
import com.ha.result.PageResult;
import com.ha.result.Result;
import com.ha.service.EmployeeService;
import com.ha.utils.JwtUtil;
import com.ha.vo.EmployeeLoginVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;

/**
 * ClassName: EmployeeController
 * Package: com.ha.controller.admin
 * Description:
 *
 * @Author 那啥啥
 * @Create 2024/3/31  17:08
 * @Version 1.0
 */
@RestController
@Api(tags = "员工相关接口")
@Slf4j
@RequestMapping("/admin/employee")
public class EmployeeController {

    @Autowired
    private EmployeeService employeeService;

    @Autowired
    private JwtProperties jwtProperties;

    /**
     * 员工登录
     * @param employeeLoginDTO
     * @return
     */
    @ApiOperation("员工登录")
    @PostMapping("/login")
    public Result<EmployeeLoginVO> login(@RequestBody EmployeeLoginDTO employeeLoginDTO) {

        log.info("员工登录{}", employeeLoginDTO);
        //校验是否有该身份
        Employee employee = employeeService.login(employeeLoginDTO);

        //登录成功生成jwt令牌
        HashMap<String, Object> claim = new HashMap<>();
        claim.put(JwtClaimsConstant.EMP_ID,employee.getId());
        String token = JwtUtil.createJWT(
                jwtProperties.getAdminSecretKey(),
                jwtProperties.getAdminTtl(),
                claim
        );
        EmployeeLoginVO employeeLoginVO = EmployeeLoginVO.builder()
                .id(employee.getId())
                .name(employee.getName())
                .token(token)
                .userName(employee.getUsername())
                .build();

        return Result.success(employeeLoginVO);
    }


    /**
     * 退出
     *
     * @return
     */
    @PostMapping("/logout")
    @ApiOperation("员工退出")
    public Result<String> logout() {
        BaseContext.removeCurrentId();
        return Result.success();
    }


    @PostMapping
    @ApiOperation("新增员工")
    public Result save(@RequestBody EmployeeDTO employeeDTO){
        log.info("新增员工信息：{}",employeeDTO);
        employeeService.save(employeeDTO);
        return Result.success();
    }

    /**
     * 员工分页查询
     * @param employeePageQueryDTO
     * @return
     */
    @GetMapping("/page")
    @ApiOperation("员工分页查询")
    public Result<PageResult> page(EmployeePageQueryDTO employeePageQueryDTO){
        log.info("员工分页查询参数为 ：{}",employeePageQueryDTO);
        PageResult pageResult = employeeService.pageQuery(employeePageQueryDTO);
        return Result.success(pageResult);
    }

    @PostMapping("/status/{status}")
    @ApiOperation("员工启用禁用")
    public Result startOrStop(@PathVariable Integer status , Long id){
        log.info("启用禁用员工账号：{}，{}",status,id);
        employeeService.startOrStop(status,id);
        return Result.success();
    }

    /**
     *
     * 根据id查询员工信息
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    @ApiOperation("员工修改回写")
    public Result<Employee> getById(@PathVariable Long id){
        Employee employee =  employeeService.getById(id);
        return Result.success(employee);
    }
    @PutMapping
    @ApiOperation("修改员工信息")
    public Result update(@RequestBody EmployeeDTO employeeDTO){
        log.info("编辑员工信息：{}",employeeDTO);
        employeeService.update(employeeDTO);
        return Result.success();
    }


    @PutMapping("/password")
    @ApiOperation("修改密码")
    public Result editPassword(@RequestBody PasswordEditDTO passwordEditDTO){
        log.info("修改密码:{}",passwordEditDTO);
        employeeService.editPassword(passwordEditDTO);
        return Result.success();
    }
}
